//
//  SearchBarView.m
//  CarLife
//
//  Created by yu on 2017/11/13.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "SearchBarView.h"
#import "Utility.h"

@interface SearchBarView ()<UITextFieldDelegate>

@end

@implementation SearchBarView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
        self.returnKeyType = UIReturnKeySearch;
        [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
        self.font = SYSTEM_FONT(13);
//        self.placeholder = @"周边 我的位置 搜索";
        UIColor *placeholderColor;
        if (@available(iOS 13.0, *)) {
            placeholderColor = UIColor.secondaryLabelColor;
            UIColor *bgColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
                if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
                    return kBackgroundColor;
                } else {
                    return UIColor.systemBackgroundColor;
                }
            }];
            self.backgroundColor = bgColor;
        } else {
            placeholderColor = kTextLightGrayColor;
            self.backgroundColor = kBackgroundColor;
        }
    
        self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"搜索 我的位置 周边" attributes:@{NSForegroundColorAttributeName:placeholderColor,NSFontAttributeName:SYSTEM_FONT(13)}];
        self.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;


        UIImageView *imgView = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"near_search")];
        self.leftView = imgView;
        self.leftViewMode = UITextFieldViewModeAlways;
        
        self.layer.borderWidth = 0.5;
        self.layer.borderColor = UIColor.clearColor.CGColor;
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 2;
        
        [self addTarget:self action:@selector(textFieldDidEditing:) forControlEvents:UIControlEventEditingChanged];
    }
    return self;
}

- (void)textFieldDidEditing:(UITextField *)textField
{
    UITextRange *range = textField.markedTextRange;
    NSString *text = [textField textInRange:range];
//    if(text.length > 0 || textField.text.length == 0) {
//        return;
//    }
    if(text.length > 0) {
        return;
    }
    self.historyRecordBlock(textField.text);
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if ([self.s_delegate respondsToSelector:@selector(searchBarDidBeginEditing:)]) {
        [self.s_delegate searchBarDidBeginEditing:self];
    }
}

- (CGRect)leftViewRectForBounds:(CGRect)bounds
{
    return CGRectMake(10, 7.5, 15, 15);
}

//编辑区域，即光标的位置
- (CGRect)editingRectForBounds:(CGRect)bounds
{
    CGRect textRect = [super editingRectForBounds:bounds];
    textRect.origin.x += 10;
    textRect.size.width -= 5;
    return textRect;
}

//设置文本显示的区域
- (CGRect)textRectForBounds:(CGRect)bounds
{
    CGRect textRect = [super editingRectForBounds:bounds];
    textRect.origin.x += 10;
    textRect.size.width -= 5;
    return textRect;
}



@end
